package com.yuanian.timingwheel.statistics;

import com.yuanian.timingwheel.model.task.base.BaseTask;
import com.yuanian.timingwheel.respository.po.LivingServerEntity;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 激活的服务器
 */
public class LivingServer {

    //运行的机器标识ID
    private Long livingServerId;

    //核心线程卡顿总秒数
    private Integer delaySeconds = 0;

    //核心线程空闲总秒数
    private Integer freeSeconds = 0;

    //ip
    private String serverIp;

    //最后响应时间
    private Date lastRespondTime;

    //启动时间
    private Date startTime;

    //已处理任务数量
    private Integer handledTaskNum;

    //正在运行中的任务数量
    private Integer runningTaskNum;

    //等待运行中的任务数量
    private Integer waitingTaskNum;

    //加载的任务数量
    private Integer loadTaskNum;

    private Map<Long, BaseTask> waitingTasks;

    private Map<Long, BaseTask> runningTasks;

    private Map<Long, BaseTask> handledTasks;

    private Map<Long, BaseTask> loadTasks;

    public LivingServer() {
        waitingTasks = new ConcurrentHashMap<>();
        runningTasks = new ConcurrentHashMap<>();
        handledTasks = new ConcurrentHashMap<>();
        loadTasks = new ConcurrentHashMap<>();
    }

    public LivingServer(Long livingServerId, String serverIp) {
        this();
        this.livingServerId = livingServerId;
        this.serverIp = serverIp;
        this.startTime = new Date();
    }

    public boolean isCompleted() {
        return waitingTasks.size() == 0 && runningTasks.size() == 0;
    }

    /**
     * 获取正在运行或等待的taskid
     * @return
     */
    public Collection<Long> getLoadTaskIds() {
        return loadTasks.keySet();
    }

    public void incDelaySeconds() {
        this.delaySeconds++;
    }

    public void refresh(Date lastRespondTime) {
        this.lastRespondTime = lastRespondTime;
        LivingServerEntity.updateLivingServer(this.livingServerId, this.lastRespondTime, this.delaySeconds);
    }

    public void setLoadTaskNum(Integer loadTaskNum) {
        this.loadTaskNum = loadTaskNum;
    }

    public Map<Long, BaseTask> getWaitingTasks() {
        return waitingTasks;
    }

    public Map<Long, BaseTask> getRunningTasks() {
        return runningTasks;
    }

    public Map<Long, BaseTask> getHandledTasks() {
        return handledTasks;
    }

    public Map<Long, BaseTask> getLoadTasks() {
        return loadTasks;
    }

    public Long getLivingServerId() {
        return livingServerId;
    }

    public Integer getDelaySeconds() {
        return delaySeconds;
    }

    public Integer getFreeSeconds() {
        return freeSeconds;
    }

    public String getServerIp() {
        return serverIp;
    }

    public Date getLastRespondTime() {
        return lastRespondTime;
    }

    public Date getStartTime() {
        return startTime;
    }

    public Integer getHandledTaskNum() {
        return handledTasks.size();
    }

    public Integer getRunningTaskNum() {
        return runningTasks.size();
    }

    public Integer getWaitingTaskNum() {
        return waitingTasks.size();
    }

    public Integer getLoadTaskNum() {
        return loadTasks.size();
    }
}
